<?

class LinkCategory {
        var $ID;
        var $name;
        var $decription;
        var $is_active;

	function getID()          { return $this->ID; }
	function getName()        { return $this->name; }
	function getDescription() { return $this->description; }
	function isActive()       { return $this->is_active; }

	function get($category_id) { // static method
		$db = new db;

		$category_id = $db->sanitize_to_db($category_id);

		if ($category_id > 0 && is_numeric($category_id)) {		
			$q = "select * from Links_Categories where ID = {$category_id}";
			$r = mysql_query($q);
			$row = mysql_fetch_assoc($r);
			
			$lc = new LinkCategory;

			if ($row['ID']) {
				foreach($row as $key => $value) {
					$lc->{$key} = $db->sanitize_from_db($value);
				}
				
				return $lc;
			}
		}
		
		// if we get down here, we've failed
		$e = new Error();
		$e->add('Invalid category ID.');
		return $e;
	}

	function add($name, $description) {
		if (!User::isAdmin())
			return false;

		$db = new db;

		$name        = $db->sanitize_to_db($name);
		$description = $db->sanitize_to_db($description);

		if (get_class($this) == "LinkCategory") {
			$parent_id = $this->getID();
		} else {
			$parent_id = 0;
		}

		if (!$name) {
			$name = '(unnamed category)';
		}

		$q = "INSERT INTO Links_Categories (name, description, parent_id) VALUES ('$name', '$description', $parent_id)";

		$r = mysql_query($q);

		if ($r) {
			$lc = LinkCategory::get(mysql_insert_id());
			return $lc;
		} else {
			$e = new Error();
			$e->add("INSERT INTO Links_Categories (name, description, parent_id) VALUES ('$name', '$description', $parent_id)");
			$e->add(mysql_error());
			return $e;
		}
	}

	function update($postArray) {
		if (!User::isAdmin())
			return Error::create("Only an administrator may edit link categories.");

		$db = new db;

		$name        = $db->sanitize_to_db($postArray['name']);
		$description = $db->sanitize_to_db($postArray['description']);

		if (!$name) {
			$name = '(untitled category)';
		}

		$q = "UPDATE Links_Categories SET name = '$name', description = '{$description}' WHERE ID = " . $this->ID;

		$r = mysql_query($q);

		if ($r)
			return true;
		else
			return Error::MySQL();
	}

	function getList($parent_id) {
		$q = "SELECT ID FROM Links_Categories WHERE parent_id = {$parent_id} ORDER BY name ASC";
		$r = mysql_query($q);

		if (!$r) {
			return Error::MySQL();
		}

		$categories = array();

		while ($row = mysql_fetch_assoc($r)) {
			$categories[] = LinkCategory::get($row['ID']);
		}

		return $categories;
	}

	function getActiveList() {
		$q = "SELECT ID FROM Links_Categories WHERE is_active = 1 ORDER BY name ASC";
		$r = mysql_query($q);

		if (!$r)
			return Error::MySQL();

		$categories = array();

		while ($row = mysql_fetch_assoc($r)) {
			$categories[] = LinkCategory::get($row['ID']);
		}

		return $categories;
	}

	function getCategoryTrail() {
		$trail = array();
		$trail[$this->ID] = $this;
		$currentParent = $this->parent_id;

		while ($currentParent > 0) {
			$q = "SELECT ID, parent_id FROM Links_Categories WHERE ID = {$currentParent}";
			$r = mysql_query($q);

			if ($r) {
				$row = mysql_fetch_assoc($r);
				$trail[$currentParent] = LinkCategory::get($row['ID']);
				$currentParent = $row['parent_id'];
			}
			else {
				$e = new Error();
				$e->add(mysql_error());
				return $e;
			}
		}

		$trail = array_reverse($trail);

		return $trail;
	}

	function activate() {
		if (!User::isAdmin())
			return Error::create("Only an administrator may activate link categories.");

		$q = "UPDATE Links_Categories SET is_active = 1 WHERE ID = {$this->ID}";
		$r = mysql_query($q);

		if (!$r)
			return Error::MySQL();

		return $r;
	}
	
	function deactivate() {
		if (!User::isAdmin())
			return Error::create("Only an administrator may deactivate link categories.");

		$q = "UPDATE Links_Categories SET is_active = 0 WHERE ID = {$this->ID}";
		$r = mysql_query($q);

		if (!$r)
			return Error::MySQL();

		return $r;
	}
	
	function remove() {
		if (!User::isAdmin())
			return Error::create("Only an administrator may remove link categories.");

		$q = "DELETE FROM Links WHERE category_id = {$this->ID}";
		$r = mysql_query($q);

		if (!$r)
			return Error::MySQL();

		$q = "DELETE FROM Links_Categories WHERE ID = {$this->ID}";
		$r = mysql_query($q);

		if (!$r)
			return Error::MySQL();

		return $r;
	}
}


class Link {

	var $ID, $name, $description, $category_id, $is_active;
	
	//static
	function getList($category_id) {
		$q = "SELECT ID FROM Links WHERE category_id = $category_id ORDER BY name ASC";
		$r = mysql_query($q);
		
		if (!$r)
			return Error::MySQL();
		
		$links = array();

		while ($row = mysql_fetch_assoc($r)) {
			$links[] = Link::get($row['ID']);
		}

		return $links;
	}
	
	function getActiveList($category_id) {
		$q = "SELECT ID FROM Links WHERE category_id = $category_id AND is_active = 1 ORDER BY name ASC";
		$r = mysql_query($q);

		if (!$r)
			return Error::MySQL();

		$links = array();

		while ($row = mysql_fetch_assoc($r)) {
			$links[] = Link::get($row['ID']);
		}

		return $links;
	}
	
	function get($linkID) {
		$q = "select ID, name, description, url, category_id, is_active from Links where ID = {$linkID}";
		$r = mysql_query($q);
		if (!$r) {
			return Error::MySQL();
		}
		
		$row = mysql_fetch_assoc($r);
		if (!$row['ID']) {
			return Error::create("Invalid Link ID.");
		}
		
		$l = new Link;
		$l->ID = $row['ID'];
		$l->name = db::sanitize_from_db($row['name']);
		$l->description = db::sanitize_from_db($row['description']);
		$l->url = db::sanitize_from_db($row['url']);
                $l->category_id = $row['category_id'];
		$l->is_active = $row['is_active'];
		
		return $l;	
	}

	function getID() {return $this->ID;}
	function getName() {return $this->name;}
	function getURL() {return $this->url;}
	function getDescription() {return $this->description;}
        function getCategoryID() { return $this->category_id; }
	function isActive() {return $this->is_active;}
	
	function deactivate() {
		if (User::isAdmin()) {
			$r = mysql_query("update Links set is_active = 0 where ID = {$this->ID}");
			if ($r) {
				return $r;
			} else {
				return Error::MySQL();
			}
		} else {
			return Error::create("Only an administrator may deactivate links.");
		}
	}
	
	function activate() {
		if (User::isAdmin()) {
			$r = mysql_query("update Links set is_active = 1 where ID = {$this->ID}");
			if ($r) {
				return $r;
			} else {
				return Error::MySQL();
			}
		} else {
			return Error::create("Only an administrator may activate links.");
		}			
	}
	
	function remove() {
		if (User::isAdmin()) {
			$r = mysql_query("delete from Links where ID = {$this->ID}");
			if ($r) {
				return $r;
			} else {
				return Error::MySQL();
			}
		} else {
			return Error::create("Only an administrator may remove links.");
		}			
	}
		
	function update($postArray) {
		if (User::isAdmin()) { 
			$db = new db;
			$name = $db->sanitize_to_db($postArray['name']);
			$description = $db->sanitize_to_db($postArray['description']);
			$url = $db->sanitize_to_db($postArray['url']);
			if (!$name) {
				$name = '(untitled link)';
			}
			if (strlen($url) < 6) {
				return Error::create("Please enter a valid url. A url typically begins with \"http://\"");
			}			
			
			$r = mysql_query("update Links set name='$name', description = '{$description}', url='$url' where ID = " . $this->ID);
			if ($r) {
				return true;
			} else {
				return Error::MySQL();
			}
			
		} else {
			return Error::create("Only an administrator may edit links.");
		}
	}
	
	function add($postArray) {
		if (User::isAdmin()) { 
			$db = new db;

			$name        = $db->sanitize_to_db($postArray['name']);
			$description = $db->sanitize_to_db($postArray['description']);
			$url         = $db->sanitize_to_db($postArray['url']);
                        $category_id = $postArray['category_id'];

			if (!$name) {
				$name = '(untitled link)';
			}
			
			if (strlen($url) < 6) {
				return Error::create("Please enter a valid URL. A URL typically begins with \"http://\"");
			}
			
			$r = mysql_query("INSERT INTO Links (name, description, url, category_id, is_active) VALUES ('$name', '$description', '$url', '$category_id', " . DEFAULT_ACTIVE . ")");
			if ($r) {
				$nl = Link::get(mysql_insert_id());
				return $nl;
			} else {
				return Error::MySQL();
			}
		} else {
			return Error::create("Only an administrator may add links.");
		}
	}
}

?>
